package com.example.myapplication.view.chart;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.myapplication.R;
import com.example.myapplication.databinding.AtyShowChartBinding;
import com.example.myapplication.utils.CollectionUtils;
import com.example.myapplication.utils.TimeUtils;
import com.example.myapplication.widgets.chart.ChartBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class ShowChartActivity extends AppCompatActivity {

    private AtyShowChartBinding mBinding;
    private boolean isVertical = true;

    public static void start(Context context) {
        context.startActivity(new Intent(context, ShowChartActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.aty_show_chart);
        mBinding.setLifecycleOwner(this);

        initView();
    }

    protected void initView() {
        mBinding.toolbarRoot.title.setText("折线图");
        mBinding.chartView.setScreenVertical(isVertical);

        setListener();
        showChart();
    }

    protected void setListener() {
        mBinding.toolbarRoot.back.setOnClickListener(v -> finish());
        mBinding.changeType.setOnClickListener(v -> {
            isVertical = !isVertical;
            mBinding.chartView.setScreenVertical(isVertical);
        });
    }

    private void showChart() {
        Random random = new Random();
        List<RecordEntity> recordEntityList = new ArrayList<>();
        RecordEntity recordEntity;
        RecordType recordType;
        List<RecordType> recordTypeList;
        String currentDate = TimeUtils.getCurrentDate();
        for (int i = 0; i < 5; i++) {
            recordEntity = new RecordEntity();
            recordEntity.setDate(TimeUtils.addDay(currentDate, i));
            recordTypeList = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                recordType = new RecordType();
                recordType.setType("类型：" + j);
                recordType.setMoney(random.nextInt(100));
                recordTypeList.add(recordType);
            }
            recordEntity.setRecordTypeList(recordTypeList);
            recordEntityList.add(recordEntity);
        }

        mBinding.chartView.clear();
        ArrayList<ChartBean> chartBeans;
        ChartBean chartBean;
        TreeMap<String, ArrayList<ChartBean>> dataMap = new TreeMap<>();
        for (RecordEntity entity : recordEntityList) {
            if (CollectionUtils.isNotEmpty(entity.getRecordTypeList()))
                for (RecordType type : entity.getRecordTypeList()) {
                    chartBeans = dataMap.computeIfAbsent(type.getType(), k -> new ArrayList<>());
                    String date = entity.getDate().split(" ")[0];
                    String[] dateSplit = date.split("/");
                    if (String.valueOf(TimeUtils.getCurrentYear()).equals(dateSplit[0])) {
                        date = String.format("%s.%s", dateSplit[1], dateSplit[2]);
                    } else {
                        date = String.format("%s.%s.%s", dateSplit[0].substring(2), dateSplit[1], dateSplit[2]);
                    }
                    chartBean = new ChartBean(date, type.getMoney());
                    chartBeans.add(chartBean);
                }
        }
        mBinding.chartView.addChartBeanList(dataMap);
    }

    static class RecordEntity {
        private String date;
        private List<RecordType> mRecordTypeList;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public List<RecordType> getRecordTypeList() {
            return mRecordTypeList;
        }

        public void setRecordTypeList(List<RecordType> recordTypeList) {
            mRecordTypeList = recordTypeList;
        }
    }

    static class RecordType {
        private String type;
        private int money;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getMoney() {
            return money;
        }

        public void setMoney(int money) {
            this.money = money;
        }
    }
}
